package cn.zhihong.controller;


import cn.zhihong.common.R;
import cn.zhihong.mapper.EmployeeMapper;
import cn.zhihong.pojo.Employee;
import cn.zhihong.service.EmployeeService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Date;


@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

//    员工登录
    @PostMapping("/login")
    public R<Employee> login(HttpSession session, @RequestBody Employee employee){

//        将页面提交的password进行md5加密处理
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
//        根据提交的用户名，查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

//        验证用户名是否存在
        if (emp == null){
            return R.error("登录失败...该用户不存在");
        }
//        密码比对，如果不一致，返回失败结果
        if (!emp.getPassword().equals(password)){
            return R.error("登录失败...密码错误");
        }
//        查看员工禁用状态，如果已禁用状态，则返回员工已禁用结果
        if (emp.getStatus() == 0){
            return R.error("账号已禁用");
        }
//        登录成功，将员工id存入session中，并返回登录结果
        session.setAttribute("employee",emp.getId());

        return R.success(emp);
    }

    @PostMapping("/logout")
    public R<String> logout(HttpSession session){
        session.removeAttribute("employee");
        return R.success("退出成功");
    }
/**
 * @author 庄志宏
 * @date: 2022/4/21 23:58
 * 添加用户
 */
    @PostMapping
    public R<String> save(HttpSession session,@RequestBody Employee employee){
        log.info("添加用户的id:{}",session.getAttribute("employee"));
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employeeService.save(employee);
        return R.success("新增员工成功");
    }
/**
 * @author 庄志宏
 * @date: 2022/4/21 18:34
 * 分页查询数据
 */
    @GetMapping("/page")
    public R<Page> selectPage(Integer page,Integer pageSize,String name){
        log.info("page={},pageSize={},name={}",page,pageSize,name);
        Page<Employee> page1 = new Page<>(page,pageSize);
        LambdaQueryWrapper<Employee> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Employee::getUpdateTime);
        lambdaQueryWrapper.like(StringUtils.isNotBlank(name),Employee::getName,name);
        employeeService.page(page1,lambdaQueryWrapper);
        return R.success(page1);
    }
    /**
     * @author 庄志宏
     * @date: 2022/4/21 18:57
     * 修改用户信息,这是一个通用的update方法
     */
    @PutMapping
    public R<String> update(@RequestBody Employee employee){
        log.info("禁用或启用员工的信息，传入的status为0则为禁用" + employee.toString());
        employee.setCreateTime(LocalDate.from(LocalDateTime.now()));
        employee.setUpdateTime(LocalDate.from(LocalDateTime.now()));
        employeeService.updateById(employee);
        return R.success("员工信息修改成功");
    }

    /**
     * @author 庄志宏
     * @date: 2022/4/21 20:36
     * 对员工信息进行编辑中，返回查询的数据
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable("id") Long id){
        log.info("根据id查询员工信息============");
        Employee employee = employeeService.getById(id);
        if (employee != null){
            return R.success(employee);
        }
        return R.error("没有查询到对应员工信息====");
    }

}
